package slot_game

import (
	"context"
	"errors"

	"gitee.com/liuxuezhan/ar-platform/assets-api/pkg/xerrors"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/query"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/slot_game"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"
	SlotRandom "gitee.com/liuxuezhan/ar-platform/base-common/random"
	ar "gitee.com/liuxuezhan/ar-platform/jwttoken"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type C_SlotGame struct {
	rpc.DataHead
	rpc.SlotGameStartRes
}

func init() {
}

type UserSlotModel struct {
	ctx context.Context
	db  *gorm.DB
	logx.Logger
	tx     *gorm.DB
	svcCtx *svc.ServiceContext
}

func NewUserSlot(ctx context.Context, svc *svc.ServiceContext) *UserSlotModel {
	return &UserSlotModel{
		svcCtx: svc,
		ctx:    ctx,
		db:     svc.Db,
		Logger: logx.WithContext(ctx),
	}
}
func (model *UserSlotModel) getDb() *gorm.DB {
	if model.db != nil {
		return model.db
	}
	return svc.Db
}
func (l *UserSlotModel) Start(in *rpc.SlotGameStartReq) (ret *rpc.SlotGameStartRes, err error) {
	UserId := ar.GetUserID(in.AuthToken)
	if UserId == 0 {
		return nil, xerrors.UserTokenInvalid
	}
	err = l.getDb().Transaction(func(tx *gorm.DB) error {
		g := query.Use(tx).SlotGame
		d, err := g.Where(g.UserID.Eq(UserId)).First()
		tmp := &C_SlotGame{}
		if errors.Is(err, gorm.ErrRecordNotFound) {
			tmp.SlotGameStartRes = rpc.SlotGameStartRes{
				Save: &rpc.SlotsGameData{
					UserID:      UserId,
					TotalAmount: 10000,
					BetSize:     0.03,
					BetLevel:    1,
					BaseBet:     25,
				},
			}
			d := &slot_game.SlotGame{}
			d.SlotsGameData = *tmp.SlotGameStartRes.Save
			g.Create(d)
			tmp.DataHead = d.DataHead
		} else {
			tmp.DataHead = d.DataHead
			tmp.Save = &d.SlotsGameData
		}
		_, ret = SlotRandom.SlotGameRand(&tmp.SlotGameStartRes)
		h := g.Where(g.ID.Eq(tmp.ID))
		h.Select(g.ALL).Debug().Updates(ret.Save)
		g2 := query.Use(tx).SlotHistory
		d2 := &slot_game.SlotHistory{
			SlotsGameData: *ret.Save,
		}
		g2.Debug().Create(d2)
		return nil
	})
	return
}

func (l *UserSlotModel) GetHistory(in *rpc.SlotGameHistoryReq) (ret *rpc.SlotGameHistoryRes, err error) {
	UserId := ar.GetUserID(in.AuthToken)
	if UserId == 0 {
		return nil, xerrors.UserTokenInvalid
	}
	if in.Page != nil {
		if in.Page.Page <= 0 {
			in.Page.Page = 1
		}
		if in.Page.PageSize <= 0 {
			in.Page.PageSize = 10
		}
		ret.Page.Page = uint32(in.Page.Page)
		ret.Page.PageSize = uint32(in.Page.PageSize)
	}
	ret = &rpc.SlotGameHistoryRes{
		Page: &rpc.Pagination{},
	}
	g := query.Use(l.getDb()).SlotHistory
	h := g.Where(g.UserID.Eq(UserId))
	if in.Page.SortOrder == "asc" {
		h = h.Order(g.CreatedAt.Asc())
	} else {
		h = h.Order(g.CreatedAt.Desc())
	}
	ret.Page.Total, err = h.Count()
	if err != nil {
		return nil, err
	}
	var list []*slot_game.SlotHistory
	list, _, err = h.FindByPage(int((in.Page.Page-1)*in.Page.PageSize), int(in.Page.PageSize))
	for _, game := range list {
		ret.History = append(ret.History, &game.SlotsGameData)
	}
	return
}
